﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace SoftVenture.Sharepoint.Permissions.UnitTest
{
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class ListHelperUnitTest
    {
        private string url = "http://www.rncci.min-saude.pt/News";
        private string docLib = "MapaImagens";
        string roleName = "Aprovar";
        string roleName2 = "Controlo Total";
        string user = @"LMONTEIRO-DEV\testeuser";

        public ListHelperUnitTest()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        [TestInitialize()]
        public void MyTestInitialize()
        {
            System.Diagnostics.Debug.WriteLine("\tInit MyTestInitialize"); 
            
            string output = string.Empty;

            ListHelper lh = new ListHelper(url, docLib);

            lh.ResetRoleInheritance(ref output);

            System.Diagnostics.Debug.WriteLine(output);

            lh.Dispose();

            System.Diagnostics.Debug.WriteLine("\tEnd MyTestInitialize");
        }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void ListPermission()
        {
            System.Diagnostics.Debug.WriteLine("\tInit ListPermission");
            string output = string.Empty;
            ListHelper lh = new ListHelper(url, docLib);

            lh.List(true, ref output);

            System.Diagnostics.Debug.WriteLine(output);

            lh.Dispose();

            System.Diagnostics.Debug.WriteLine("\tEnd ListPermission");
        }

        #region Single File Tests
        [TestMethod]
        public void AddUserRole()
        {
            System.Diagnostics.Debug.WriteLine("\tInit AddUserRole");
            string output = string.Empty;

            ListHelper lh = new ListHelper(url, docLib);

            lh.BreakRoleInheritance(false, ref output);

            lh.Add(roleName, user, ref output);

            lh.List(false, ref output);

            lh.Dispose();

            System.Diagnostics.Debug.WriteLine(output);

            Assert.IsTrue(output.IndexOf(user + Environment.NewLine + "\t\t" + roleName) > 0);

            System.Diagnostics.Debug.WriteLine("\tEnd AddUserRole");
        }
        [TestMethod]
        public void AddUser2Roles()
        {
            System.Diagnostics.Debug.WriteLine("\tInit AddUser2Roles");
            string output = string.Empty;

            ListHelper lh = new ListHelper(url, docLib);

            lh.BreakRoleInheritance(false, ref output);

            lh.Add(roleName, user, ref output);
            lh.Add(roleName2, user, ref output);

            lh.List(false, ref output);

            lh.Dispose();

            System.Diagnostics.Debug.WriteLine(output);

            Assert.IsTrue(
                output.IndexOf(user + Environment.NewLine + "\t\t" + roleName + Environment.NewLine + "\t\t" + roleName2) > 0 ||
                output.IndexOf(user + Environment.NewLine + "\t\t" + roleName2 + Environment.NewLine + "\t\t" + roleName) > 0);
            
            System.Diagnostics.Debug.WriteLine("\tEnd AddUser2Roles");
        }
        [TestMethod]
        public void RemoveSingleRoleUser()
        {
            System.Diagnostics.Debug.WriteLine("\tInit RemoveSingleRoleUser");

            string output = string.Empty;
            string result = string.Empty;

            ListHelper lh = new ListHelper(url, docLib);

            lh.BreakRoleInheritance(false, ref output);

            lh.Add(roleName, user, ref output);

            lh.List(false, ref output);
            lh.List(false, ref result);

            System.Diagnostics.Debug.WriteLine(output);

            Assert.IsTrue(result.IndexOf(user + Environment.NewLine + "\t\t" + roleName) > 0);

            result = string.Empty;

            lh.Remove(roleName, user, ref output);

            lh.List(false, ref output);
            lh.List(false, ref result);

            lh.Dispose();

            System.Diagnostics.Debug.WriteLine(output);

            Assert.IsTrue(result.IndexOf(user + Environment.NewLine + "\t\t" + roleName) == -1);

            System.Diagnostics.Debug.WriteLine("\tEnd RemoveSingleRoleUser");
        }
        [TestMethod]
        public void Remove1o2Roles()
        {
            System.Diagnostics.Debug.WriteLine("\tInit Remove1o2Roles");

            string output = string.Empty;
            string result = string.Empty;

            ListHelper lh = new ListHelper(url, docLib);

            lh.BreakRoleInheritance(false, ref output);

            lh.Add(roleName, user, ref output);
            lh.Add(roleName2, user, ref output);

            lh.List(false, ref output);
            lh.List(false, ref result);

            Assert.IsTrue(
                result.IndexOf(user + Environment.NewLine + "\t\t" + roleName + Environment.NewLine + "\t\t" + roleName2) > 0 ||
                result.IndexOf(user + Environment.NewLine + "\t\t" + roleName2 + Environment.NewLine + "\t\t" + roleName) > 0);

            output = string.Empty;

            lh.Remove(roleName, user, ref output);

            lh.List(false, ref output);
            lh.List(false, ref result);

            lh.Dispose();

            System.Diagnostics.Debug.WriteLine(output);

            Assert.IsTrue(
                result.IndexOf(user + Environment.NewLine + "\t\t" + roleName) == -1 &&
                result.IndexOf(user + Environment.NewLine + "\t\t" + roleName2) > 0 - 1
                );
            
            System.Diagnostics.Debug.WriteLine("\tEnd Remove1o2Roles");
        }
        [TestMethod]
        public void RemoveAllRoleFromUser()
        {
            System.Diagnostics.Debug.WriteLine("\tInit RemoveAllRoleFromUser");
            string output = string.Empty;
            string result = string.Empty;

            ListHelper lh = new ListHelper(url, docLib);

            lh.BreakRoleInheritance(false, ref output);

            lh.Add(roleName, user, ref output);

            lh.List(false, ref output);
            lh.List(false, ref result);

            Assert.IsTrue(result.IndexOf(user + Environment.NewLine + "\t\t" + roleName) > 0);

            result = string.Empty;

            lh.RemoveAll(user, ref output);

            lh.List(false, ref output);
            lh.List(false, ref result);

            lh.Dispose();

            System.Diagnostics.Debug.WriteLine(output);

            Assert.IsTrue(result.IndexOf(user + Environment.NewLine + "\t\t" + roleName) == -1);

            System.Diagnostics.Debug.WriteLine("\tEnd RemoveAllRoleFromUser");
        }
        [TestMethod]
        public void Remove2o2RolesFromUser()
        {
            System.Diagnostics.Debug.WriteLine("\tInit Remove2o2RolesFromUser");
            
            string output = string.Empty;
            string result = string.Empty;

            ListHelper lh = new ListHelper(url, docLib);

            lh.BreakRoleInheritance(false, ref output);

            lh.Add(roleName, user, ref output);
            lh.Add(roleName2, user, ref output);

            lh.List(false, ref output);
            lh.List(false, ref result);

            Assert.IsTrue(
                result.IndexOf(user + Environment.NewLine + "\t\t" + roleName + Environment.NewLine + "\t\t" + roleName2) > 0 ||
                result.IndexOf(user + Environment.NewLine + "\t\t" + roleName2 + Environment.NewLine + "\t\t" + roleName) > 0);

            result = string.Empty;

            lh.RemoveAll(user, ref output);

            lh.List(false, ref output);
            lh.List(false, ref result);

            lh.Dispose();

            System.Diagnostics.Debug.WriteLine(output);

            Assert.IsTrue(
                result.IndexOf(user + Environment.NewLine + "\t\t" + roleName + Environment.NewLine + "\t\t" + roleName2)== -1 ||
                result.IndexOf(user + Environment.NewLine + "\t\t" + roleName2 + Environment.NewLine + "\t\t" + roleName) == -1);

            System.Diagnostics.Debug.WriteLine("\tEnd Remove2o2RolesFromUser");
        }
        #endregion

    }
}
